package com.ohuang.downloaddemo;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.ohuang.download.AbstractDownloadTask;
import com.ohuang.download.DownLoadListener;
import com.ohuang.download.DownLoadManager;
import com.ohuang.download.DownloadCallBack;
import com.ohuang.download.OhDownLoad;
import com.ohuang.download_okhttp.OkDownloadClient;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

  ViewGroup layout;
static DownLoadManager downLoadManager;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        downLoadManager=new DownLoadManager(getFilesDir().getPath(),new OhDownLoad(new OkDownloadClient()));
        layout = findViewById(R.id.ll_main_Null);
        ArrayList<DownloadBean> downloadBeans = new ArrayList<>();
        downloadBeans.add(new DownloadBean(
                this,
                "https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4",
                Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOWNLOADS + "/a.mp4"
        ));
        downloadBeans.add(new DownloadBean(
                this,
                "http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4",
                Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOWNLOADS + "/b.mp4"
        ));
        for (DownloadBean downloadBean : downloadBeans) {

                layout.addView(downloadBean.view);

        }
    }

    private static class DownloadBean {
        public static final String TAG="DownloadBean";
        public View view;
        private TextView tv_process;
        private TextView tv_sate;
        private TextView tv_url;
        private TextView tv_file;
        private Button btn_start;
        private Button btn_stop;
        private Button btn_delete;
        Context context;
        String url;
        String filePath;
        AbstractDownloadTask download;

        public DownloadBean(Context context, String url, String filePath) {
            this.context = context;
            this.url = url;
            this.filePath = filePath;
            view = LayoutInflater.from(context).inflate(R.layout.item_download, null, false);
            tv_process = view.findViewById(R.id.tv_process);
            tv_sate = view.findViewById(R.id.tv_state);
            tv_url = view.findViewById(R.id.tv_url);
            tv_file = view.findViewById(R.id.tv_filePath);
            btn_start = view.findViewById(R.id.btn_start);
            btn_stop = view.findViewById(R.id.btn_stop);
            btn_delete = view.findViewById(R.id.btn_delete);
            initView();
        }

        private void initView() {
            tv_url.setText(url);
            tv_file.setText(filePath);
            btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    File file = new File(filePath);
                    file.delete();
                }
            });

            btn_start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (download == null) {
                        download = downLoadManager.get().download(url, new File(filePath), new DownloadCallBack() {
                            @Override
                            public void onStart(AbstractDownloadTask downloadTask) {
                                super.onStart(downloadTask);
                                tv_sate.setText("开始");
                            }

                            @Override
                            public void onProgress(AbstractDownloadTask downloadTask,long progress, long total) {
                                tv_process.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        String p = "%";
                                        if (total > 0) {
                                            p = progress * 100 / total + "%";
                                        }
                                        tv_process.setText(progress + "-" + total + "  " + p);
                                    }
                                });
                            }

                            @Override
                            public void onError(AbstractDownloadTask downloadTask,Exception e) {
                                tv_sate.setText(e.toString());
                            }

                            @Override
                            public void onSuccess(AbstractDownloadTask downloadTask) {
                                tv_sate.setText("完成");
                            }

                            @Override
                            public void onStop(AbstractDownloadTask downloadTask) {
                                super.onStop(download);
                                tv_sate.setText("停止");
                            }


                        });


                        
                    }else {
                        boolean stop = download.isStop();
                        if (stop) {
                            download.continueDownload();
                        }
                        Log.d(TAG, "onClick: ");
                    }
                }
            });

            btn_stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (download!=null){
                        download.stop();
                    }
                }
            });

        }
    }

}